package sdk

import (
	"time"

	"gitee.com/youkelike/ziam/zsdk/sdk/log"
	tlsutil "gitee.com/youkelike/ziam/zutils/tls"
)

var (
	defaultScheme      = "http"
	defaultEndpoint    = "127.0.0.1:9090"
	defaultServiceName = "authz"
	defaultTimeout     = 30 * time.Second
	defaultSignMethod  = "jwt"
	secretID           = ""
	secretKey          = ""
	defaultLogLevel    = log.WarnLevel
	defaultInsecure    = true
)

// 底层客户端的配置对象
type Config struct {
	// 协议
	Scheme string
	// IP 和端口
	Endpoint    string
	Timeout     time.Duration
	LogLevel    log.Level
	signMethod  string
	ServiceName string
	Credential  *Credential
	BearerToken string
	tlsutil.TLSClientConfig
}

func NewConfig() *Config {
	return &Config{
		Scheme:      defaultScheme,
		Endpoint:    defaultEndpoint,
		Timeout:     defaultTimeout,
		signMethod:  defaultSignMethod,
		ServiceName: defaultServiceName,
		LogLevel:    defaultLogLevel,
		Credential: &Credential{
			SecretID:  secretID,
			SecretKey: secretKey,
		},
		TLSClientConfig: tlsutil.TLSClientConfig{
			Insecure: defaultInsecure,
		},
	}
}

func (c *Config) SetScheme(scheme string) *Config {
	c.Scheme = scheme
	return c
}

func (c *Config) SetEndpoint(endpoint string) *Config {
	c.Endpoint = endpoint
	return c
}

func (c *Config) SetTimeout(timeout time.Duration) *Config {
	c.Timeout = timeout
	return c
}

func (c *Config) SetLogLevel(level log.Level) *Config {
	c.LogLevel = level
	return c
}

func (c *Config) SetSignMethod(signMethod string) *Config {
	c.signMethod = signMethod
	return c
}

func (c *Config) SetServiceName(ServiceName string) *Config {
	c.ServiceName = ServiceName
	return c
}

func (c *Config) SetCredential(cred *Credential) *Config {
	c.Credential = cred
	return c
}

func (c *Config) SetSecret(secretID, secretKey string) *Config {
	c.Credential = NewCredentials(secretID, secretKey)
	return c
}

func (c *Config) SetBearerToken(token string) *Config {
	c.BearerToken = token
	return c
}

func (c *Config) SetInsecure(Insecure bool) *Config {
	c.Insecure = Insecure
	return c
}
func (c *Config) SetServerName(ServerName string) *Config {
	c.ServerName = ServerName
	return c
}
func (c *Config) SetSecureHost(SecureHost string) *Config {
	c.SecureHost = SecureHost
	c.Endpoint = SecureHost
	c.Scheme = "https"
	return c
}
func (c *Config) SetCertFile(CertFile string) *Config {
	c.CertFile = CertFile
	return c
}
func (c *Config) SetKeyFile(KeyFile string) *Config {
	c.KeyFile = KeyFile
	return c
}
func (c *Config) SetCAFile(CAFile string) *Config {
	c.CAFile = CAFile
	return c
}
